package parser;

import entity.cpe.Cpe;

/**
 * 解析cpe2.3版本的uri工具类
 *
 * @author xym
 * @since 2020/7/15
 */
public class CpeUriParser {
    private static CpeUriParser parser;

    private CpeUriParser() {
    }

    public static CpeUriParser getParser() {
        if (parser == null) {
            parser = new CpeUriParser();
        }
        return parser;
    }

    public Cpe execute(String cpe23Uri) {
        cpe23Uri = cpe23Uri.replace("\\:", "~");

        String[] attributes = cpe23Uri.split(":");
        Cpe cpe = new Cpe();
        cpe.setCpeVersion(attributes[1]);

        if ("a".equals(attributes[2])) {
            cpe.setPart("application");
        } else if ("o".equals(attributes[2])) {
            cpe.setPart("operating system");
        } else {
            cpe.setPart("hardware device");
        }

        cpe.setVendor(attributes[3].toLowerCase().replace("\\", "").replace("~", ":"));
        cpe.setProduct(attributes[4].toLowerCase().replace("\\", "").replace("~", ":"));
        cpe.setVersion(attributes[5]);
        cpe.setUpdate(attributes[6]);
        cpe.setEdition(attributes[7]);
        return cpe;
    }
}

